<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Store extends CI_Controller {
	
	function __construct() {
		parent::__construct ();
	
	}
	
	public function index() {
		$eID = (int)$this->input->get('id');
		
		if(!isset($eID) || $eID == ''){
			$this->load->view('error');
			return ;
		}
		$this->db->where('id',$eID);
		$store = $this->db->get('store')->row_array();
		if(!isset($eID) || $store == null){
			$this->load->view('error');
			return ;
		}
		$eData = array ();
		$eData ['eID'] = $eID;
		$eData['store'] = $store;
		
		//同条街道上的小店
		$this->db->where('street',$store['street']);
		$this->db->where_not_in('id', array($eID));
		$this->db->limit(5,0);
		$eData['storeList'] = $this->db->get('store')->result_array();
		
		//小店下的图片
		$this->db->where('type',3);
		$this->db->where('belongId',$eID);
		$this->db->limit(3);
		$eData['photoList'] = $this->db->get('picture')->result_array();
		
		//小店下的评论
		$this->db->select('comment.*,member.username,member.avatar,member.id as memberId');
		$this->db->from('comment');
		$this->db->join('member', 'member.id = comment.creatid');
		
		$this->db->where('type',2);
		$this->db->where('belongId',$eID);
		$this->db->order_by('createtime', 'DESC');
		$this->db->limit(6,0);
		$commentList = $this->db->get()->result_array();
		/*
		$commentList = array();
		foreach($commentListTemp as $key => $comment){
			$this->db->select('comment.*,member.username,member.avatar,member.id as memberId');
			$this->db->from('comment');
			$this->db->join('member', 'member.id = comment.creatid');
			$this->db->where('type',4);
			$this->db->where('belongId',$comment['id']);
			$this->db->order_by('createtime', 'DESC');
			$this->db->limit(6,0);
			$replyList = $this->db->get()->result_array();
			$comment['replyList'] = $replyList;
			$commentList[] = $comment;
		}
		*/
		 $eData['commentList'] = $commentList;
		//小店下的公告
		$this->db->where('type',2);
		$this->db->where('belongId',$eID);
		$this->db->limit(7,0);
		$eData['announceList'] = $this->db->get('announce')->result_array();
		
		//标签
		$this->db->where('type',1);
		$this->db->where('belongId',$eID);
		$eData['tagList'] = $this->db->get('tag')->result_array();
		
		//收藏小店的会员
		$this->db->select("member.*");
		$this->db->from("member");
		$this->db->join("favorite","favorite.member=member.id");
		$this->db->where('favoriteId',$eID);
		$this->db->where('favoriteType',2);
		$eData['favList'] = $this->db->get()->result_array();
		
		
		//友情链接
		$this->db->order_by ( 'sort', 'asc' );
		$this->db->order_by ( 'id', 'desc' );
		$this->db->where ( 'cid', 4 );
		$eData['friendlink'] = $this->db->get ('adv',4,0)->result_array ();
		
		
		$this->load->view ( 'store', $eData );
	}
	
	function supports(){
		$eData = array();
		$member = $this->session->userdata ( 'member' );
		$eData['typeId'] = $this->input->post ( 'suptId' );
		$eData['type'] = $this->input->post ( 'suptype' );
		$eData['creater'] = $member['id'];
		
		$this->db->where('typeId',$eData['typeId']);
		$this->db->where('type',$eData['type']);
		$this->db->where('creater',$eData['creater']);
		$loveCount = $this->db->count_all_results('support');
		
		if($loveCount > 0){
			$ret ['success'] = 'false';
			if('recommends' == $eData['type']){
				$ret ['msg'] = '你已经推荐过了';
			}else{
				$ret ['msg'] = '你已经支持过了';
			}
			
			echo json_encode ( $ret );
		}else{
			$eData['createTime'] = time();
			
			$this->db->insert ( 'support',$eData );
			
			$eData['suptId'] = $this->input->post ( 'suptId' );
			$eData['suptype'] = $this->input->post ( 'suptype' );
			$this->db->query('UPDATE xd_store SET '.$eData['suptype'].'='.$eData['suptype'].'+1 WHERE id='.$eData['suptId']);
			header ( "Content-Type: text/html; charset=utf-8" );
			
			$ret ['success'] = 'true';
			$ret ['msg'] = '支持成功';
			echo json_encode ( $ret );
		}
		/*
		
		$eData = array();
		$member = $this->session->userdata ( 'member' );
		$eData['suptId'] = $this->input->post ( 'suptId' );
		$eData['suptype'] = $this->input->post ( 'suptype' );
		
		$this->db->query('UPDATE xd_store SET '.$eData['suptype'].'='.$eData['suptype'].'+1 WHERE id='.$eData['suptId']);
		
		
		header ( "Content-Type: text/html; charset=utf-8" );
		
		$ret ['success'] = 'true';
		$ret ['msg'] = '支持成功';
		echo json_encode ( $ret );
		*/
	}
	
}

